﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using CLEX.Service.DataContracts;

namespace CLEX.Client.Metro.ViewModel.Login
{
    public sealed class LoginVM : VMBase
    {
        private readonly IFacade facade;
        private string username = "dmklimek";
        private string password = "p";
        private readonly RelayCommand login;
        private string usernameErrors;
        private string passwordErrors;
        private string loginErrors;

        public ICommand Login
        {
            get { return login; }
        }

        public string Username
        {
            get { return username; }
            set
            {
                if (username != value)
                {
                    username = value;
                    RaisePropertyChanged("Username");
                    this.login.RaiseCanExecuteChanged();
                }
            }
        }

        public string Password
        {
            get { return password; }
            set
            {
                if (password != value)
                {
                    password = value;
                    RaisePropertyChanged("Password");
                    this.login.RaiseCanExecuteChanged();
                }
            }
        }

        public string UsernameErrors
        {
            get { return usernameErrors; }
            private set
            {
                if (usernameErrors != value)
                {
                    usernameErrors = value;
                    RaisePropertyChanged("UsernameErrors");
                }
            }
        }

        public string PasswordErrors
        {
            get { return passwordErrors; }
            private set
            {
                if (passwordErrors != value)
                {
                    passwordErrors = value;
                    RaisePropertyChanged("PasswordErrors");
                }
            }
        }

        public string LoginErrors
        {
            get { return loginErrors; }
            private set
            {
                if (loginErrors != value)
                {
                    loginErrors = value;
                    RaisePropertyChanged("LoginErrors");
                }
            }
        }

        public LoginVM(IFacade facade)
        {
            this.facade = facade;
            this.login = new RelayCommand(DoLogin, CanLogin);
            this.login.CanExecuteChanged += Login_CanExecuteChanged;
            Login_CanExecuteChanged(null, null);
        }

        private void Login_CanExecuteChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(username))
            {
                UsernameErrors = "Please enter a username.";
            }
            else
            {
                UsernameErrors = null;
            }
            if (string.IsNullOrEmpty(password))
            {
                PasswordErrors = "Please enter a password.";
            }
            else
            {
                PasswordErrors = null;
            }
        }

        private void DoLogin()
        {
            LoginErrors = string.Empty;

            UserData user;
            try
            {
                user = facade.Login(username, password);
            }
            catch (Exception ex)
            {
                facade.ShowUnexpectedError("An unexpected error occured while logging in.", ex.Message);
                return;
            }

            if (user != null)
            {
                facade.GoToHome(user);
            }
            else
            {
                LoginErrors = "Invalid username or password.";
            }
        }

        private bool CanLogin()
        {
            return !string.IsNullOrWhiteSpace(username) && !string.IsNullOrEmpty(password);
        }
    }
}
